package com.until;

import com.model.MyUser;
import com.service.MyuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
@EnableScheduling
public class AlarmTask {
    private static final SimpleDateFormat myFormat=new SimpleDateFormat("HH:mm:ss");

    private static int onlineCounter =0;

    /*间隔5秒完成一次， cron默认定时执行的表达方式*/
    @Scheduled(cron = "0/5 * * * * *")
    public void run() throws InterruptedException{
        Thread.sleep(6000);
        System.out.println(onlineCounter++);
        System.out.println("cron,当前时间："+myFormat.format(new Date()));
        System.out.println(Thread.currentThread().getName()+"=====>"+System.currentTimeMillis()/1000);
    }

    /**/
    @Scheduled(fixedRate = 5000)
    public void run1() throws InterruptedException {
        Thread.sleep(6000);
        System.out.println("fixedRate,当前时间："+myFormat.format(new Date()));
        System.out.println(Thread.currentThread().getName()+"=====>"+System.currentTimeMillis()/1000);
    }

    /**/
    @Scheduled(fixedDelay = 5000)
    public void run2() throws InterruptedException {
        Thread.sleep(6000);
        System.out.println("fixedDelay,当前时间："+myFormat.format(new Date()));
        System.out.println(Thread.currentThread().getName()+"=====>"+System.currentTimeMillis()/1000);
    }

    @Scheduled(initialDelay = 2000,fixedDelay = 5000)
    public void run3() throws InterruptedException {
        System.out.println("initialDelay,当前时间："+myFormat.format(new Date()));
        System.out.println(Thread.currentThread().getName()+"=====>"+System.currentTimeMillis()/1000);
        System.out.println("fixedDelay,当前时间"+myFormat.format(new Date()));
    }
}
